<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\Cate;
use app\common\model\Type;
use think\exception\HttpException;
use think\facade\View;

class Goods extends Base
{
    /**
     * 商品列表展示
     * 条件搜索
     * @param \app\common\model\Goods $goods
     * @return string
     * @throws \think\db\exception\DbException
     */
    public function goodsList (\app\common\model\Goods $goods){
        $keyword = request()->param('keyword');
        $list = $goods->when($keyword,function ($query,$keyword){
            $query->where('goods_name','like',"%$keyword%");
        })->paginate([
            'list_rows' => 8,
            'query' => ['keyword' => $keyword]
        ]);
        View::assign('list',$list);
        return View::fetch('goods/product-list',['query' => ['keyword' => $keyword]]);
    }

    /**
     * 商品添加展示分类和模型列表
     * @param Cate $cate
     * @param Type $type
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsAdd (Cate $cate,Type $type){
        $arr_cate = $cate->field('id,cate_name')->where('pid',0)->select();
        $arr_type = $type->field('id,type_name')->select();
        View::assign('category',$arr_cate);
        View::assign('type',$arr_type);
        return View::fetch('goods/product-add');
    }
}
